package com.zsgeek.wisdchain.qutu.tools;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.opengl.GLES10;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.BaseRequestOptions;

import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.channels.FileChannel;

/**
 * Created by whb on 2016/8/29.
 */
public class ImageUtils {

    /**
     * 加载图片
     */
    public static void loadImage(Context context, String url, final ImageView imageView) {
        if (imageView == null) return;
        Glide.with(context).load(url).diskCacheStrategy(DiskCacheStrategy.ALL).into(imageView);
    }

    public static void loadImage(Activity activity, String url, final ImageView imageView) {
        if (imageView == null) return;
        Glide.with(activity).load(url).diskCacheStrategy(DiskCacheStrategy.ALL).into(imageView);
    }

    /**
     * 显示圆角图片
     *
     * @param activity
     * @param url
     * @param requestOptions .apply(requestOptions)或者.apply(RequestOptions.bitmapTransform(new RoundedCorners(20)))//圆角半径
     * @param imageView
     */
    public static void loadImageCircleCrop(Activity activity, String url, @NonNull BaseRequestOptions<?> requestOptions, final ImageView imageView) {
        if (imageView == null) return;
        Glide.with(activity).load(url).apply(requestOptions).diskCacheStrategy(DiskCacheStrategy.ALL).into(imageView);
    }

    public static void loadImage(Fragment fragment, String url, final ImageView imageView) {
        if (imageView == null) return;
        Glide.with(fragment).load(url).diskCacheStrategy(DiskCacheStrategy.ALL).into(imageView);
    }

    public static void loadImage(Activity activity, int res, ImageView imageView) {
        if (imageView == null) return;
        Glide.with(activity).load(res).diskCacheStrategy(DiskCacheStrategy.ALL).into(imageView);
    }

    public static int getMaxSize() {
        int maxSize = 2048;
        int[] arr = new int[1];
        GLES10.glGetIntegerv(GLES10.GL_MAX_TEXTURE_SIZE, arr, 0);
        if (arr[0] > 0) {
            maxSize = Math.min(arr[0], 4096);
        }
        return maxSize;
    }

    public static Bitmap decodeSampledBitmapFromUri(Context context, Uri sourceUri, int requestSize) {
        InputStream stream = null;
        Bitmap bitmap = null;
        try {
            stream = context.getContentResolver().openInputStream(sourceUri);
            if (stream != null) {
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = ImageUtils.calculateInSampleSize(context, sourceUri, requestSize);
                options.inJustDecodeBounds = false;
                bitmap = BitmapFactory.decodeStream(stream, null, options);
            }
        } catch (FileNotFoundException e) {
            LogUtil.e(e.getMessage());
        } finally {
            try {
                if (stream != null) {
                    stream.close();
                }
            } catch (IOException e) {
                LogUtil.e(e.getMessage());
            }
        }
        return bitmap;
    }


    public static int calculateInSampleSize(Context context, Uri sourceUri, int requestSize) {
        InputStream is = null;
        // check image size
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        try {
            is = context.getContentResolver().openInputStream(sourceUri);
            BitmapFactory.decodeStream(is, null, options);
        } catch (FileNotFoundException ignored) {
        } finally {
            closeQuietly(is);
        }
        int inSampleSize = 1;
//        sInputImageWidth = options.outWidth;
//        sInputImageHeight = options.outHeight;
        while (options.outWidth / inSampleSize > requestSize
                || options.outHeight / inSampleSize > requestSize) {
            inSampleSize *= 2;
        }
        return inSampleSize;
    }

    public static void closeQuietly(Closeable closeable) {
        if (closeable == null) return;
        try {
            closeable.close();
        } catch (Throwable ignored) {
            ignored.printStackTrace();
        }
    }

    /**
     * 保存图片至SD卡
     *
     * @param imagePath 图片url路径
     * @param saveFile  存储位置
     * @throws
     */
    public static void saveFileToDownloads(String imagePath, File saveFile) throws Exception {
        FileInputStream inStream = new FileInputStream(new File(imagePath));
        FileOutputStream outStream = new FileOutputStream(saveFile);
        FileChannel inChannel = inStream.getChannel();
        FileChannel outChannel = outStream.getChannel();
        inChannel.transferTo(0, inChannel.size(), outChannel);
        inStream.close();
        outStream.close();
    }

}